<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Db;
use think\Model as ThinkModel;

//设置商品----商品表
class Goods extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_GOODS__';
    protected $name = 'qt_goods';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     *  商品列表
     **/
    public function productList($store_id = 0,$cate_id = 0,$keyword = '',$limit = 10)
    {
        $where = [];

        //门店ID查询
        if(!empty($store_id))
        {
            $where[] = ['store_id','eq',$store_id];
        }

        //分类查询
        if(!empty($cate_id))
        {
            $where[] = ['goods_category_id','eq',$cate_id];
        }

        //关键词查询
        if(!empty($keyword))
        {
            $where[] = ['name','like','%'.$keyword.'%'];
        }

        $where[] = ['status','eq',1];
        $list  = $this->where($where)
            ->field('id,name,price,img')
            ->order('create_time desc')
            ->paginate($limit)
            ->each(function($item){
                $item['img'] = get_img_path($item['img']);
                return $item;
            });

        return $list->items();
    }


    /**
     *  商品详情
     **/
    public function productInfo($store_id = 0,$product_id)
    {
        $where[] = ['id','eq',$product_id];
        $where[] = ['status','eq',1];
        if(!empty($store_id))
        {
            $where[] = ['store_id','eq',$store_id];
        }

        $info = $this->where($where)->find();
        if($info)
        {
            $info['img'] = get_img_path($info['img']);
        }

        return $info;
    }


    /**
     * 商品下单减库存
     * @param $goodsId
     * @param $number
     */
    public function goodsProductReduce($goodsId,$number){
        $data = Db::name('qt_goods_product')->where(['goods_id'=>$goodsId])->find();
        if(!empty($data)){
            $productData = json_decode($data['product_data'],true);
            foreach ($productData as $key=>$value){
                $stockNumber = $number * $value['number'];
                Db::name('qt_product')->where(['id'=>$value['product_id']])->setDec('stock',$stockNumber);
            }
        }
    }


    /**
     * 检测商品是否可以删除
     * @param $goodsId
     */
    public static function checkDel($goodsId){
        //套餐检测
        $package  = Db::name('qt_data_package_item')->where(['item_id'=>$goodsId,'item_type'=>'goods'])->find();
        if($package != null){
            return ['code'=>204,'msg'=>'套餐中有此商品，无法删除'];
        }
        //组合下单检测
        $combine  = Db::name('qt_data_combine_goods')->where(['goods_id'=>$goodsId])->find();
        if($combine != null){
            return ['code'=>204,'msg'=>'组合下单中有此商品，无法删除'];
        }
        //项目减免检测
        $minus  = Db::name('qt_data_minus_item')->where(['item_id'=>$goodsId,'item_type'=>'goods'])->find();
        if($minus != null){
            return ['code'=>204,'msg'=>'项目减免中有此商品，无法删除'];
        }

        //活动检测
        $activity  = Db::name('qt_data_activity_item')->where(['item_id'=>$goodsId,'item_type'=>'goods'])->find();
        if($activity != null){
            return ['code'=>204,'msg'=>'活动中有此商品，无法删除'];
        }
        //门票限制
        $admission  = Db::name('qt_admission_ticket')->where("FIND_IN_SET(" . $goodsId . ",product_ids)")->find();
        if($admission != null){
            return ['code'=>204,'msg'=>'门票中有此商品，无法删除'];
        }
        //段位费
        $duanwei  = Db::name('qt_duanwei')->where(['product_id'=>$goodsId])->find();
        if($duanwei != null){
            return ['code'=>204,'msg'=>'段位费有此商品，无法删除'];
        }
        //最大商品数限制
        $handGoods  = Db::name('qt_hand_goods')
            ->where(['product_id'=>$goodsId])->find();
        if($handGoods != null){
            return ['code'=>204,'msg'=>'手牌最大商品数有此商品，无法删除'];
        }
        //次卡限制
        $cardNumber  = Db::name('qt_viper_number_card_template_item')
            ->alias('item')
            ->leftJoin('qt_viper_number_card_template t','t.id = item.number_card_template_id')
            ->where(['item_id'=>$goodsId,'item_type'=>'goods'])->find();
        if($cardNumber != null){
            return ['code'=>204,'msg'=>'次卡【'.$cardNumber['name'].'】有此商品，无法删除'];
        }
        //月卡限制
        $cardMonth  = Db::name('qt_viper_month_card_template_item')
            ->alias('item')
            ->leftJoin('qt_viper_month_card_template t','t.id = item.month_template_id')
            ->where(['item_id'=>$goodsId,'item_type'=>'goods'])->find();
        if($cardMonth != null){
            return ['code'=>204,'msg'=>'月卡【'.$cardNumber['name'].'】有此商品，无法删除'];
        }
        return ['code'=>200];

    }

}